package com.Factory;

public class Shelf {
    public int apple = 0;

    public boolean flag = false;

    public synchronized void increase(){
        while (flag) {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        apple+=2;
        System.out.println(Thread.currentThread().getName()+"-------生产了"+apple+"个苹果");
        flag=true;
        notifyAll();
    }

    public synchronized void decrease(){
        while (!flag){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        apple--;
        System.out.println(Thread.currentThread().getName()+"-------消费了"+apple+"个苹果");
        flag=false;
        notifyAll();
    }

    public static void main(String [] args){
        Shelf shelf = new Shelf();

        Fac1 fac1 = new Fac1(shelf);
        Fac2 fac2 = new Fac2(shelf);

        Cus1 cus1 = new Cus1(shelf);
        Cus2 cus2 = new Cus2(shelf);
        Cus3 cus3 = new Cus3(shelf);

        Thread t1 = new Thread(fac1);
        Thread t2 = new Thread(fac2);
        Thread t3 = new Thread(cus1);
        Thread t4 = new Thread(cus2);
        Thread t5 = new Thread(cus3);

        t1.start();
        t2.start();
        t3.start();
        t4.start();
        t5.start();

    }
}
